package com.imc.api.shopmall.controller;

import com.imc.api.shopmall.pojo.ShopMall;
import com.imc.api.shopmall.service.ShopMallService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * Created by tanweiping on 2018/9/25.
 */
@RestController
@RequestMapping("/api/shopmall")
public class ShopMallController {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    ShopMallService shopMallService;

    /*
     * tanweiping  2018/9/25 16:00
     * 方法描述：获取购物车列表
     */
    @GetMapping("")
    ResultDTO getFromShopMall(Principal principal) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    t.setData(
                            shopMallService.getShopMallList(principal)
                    );
                },
                (e, t) -> {
                    e.printStackTrace();
                    t.setIsSuccess(false);
                    t.setMessage("获取购物车失败");
                }
        );
    }

    /*
     * tanweiping  2018/9/25 16:00
     * 方法描述：添加购物车
     */
    @PostMapping("")
    ResultDTO postMall(Principal principal, @RequestBody ShopMall shopMall) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    try {
                        t.setData(
                                shopMallService.postMall(principal, shopMall)
                        );
                    } catch (Exception e) {
                        e.printStackTrace();
                        t.setIsSuccess(false);
                        t.setMessage(e.getMessage());
                    }
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("添加购物车失败");
                }
        );
    }

    /*
     * tanweiping  2018/9/25 16:01
     * 方法描述：删除购物车
     */
    @DeleteMapping("")
    ResultDTO deleteByIds(Principal principal, @RequestBody List <Integer> ids) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    shopMallService.deleteByIds(ids);
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("删除购物车异常");
                }
        );
    }

    /*
     * tanweiping  2018/9/25 16:02
     * 方法描述：清空购物车
     */
    @DeleteMapping("/cleanAll")
    ResultDTO cleanAll(Principal principal) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    shopMallService.cleanAll(principal);
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("清空购物车异常");
                }
        );
    }
}